package com.itheima.sh.h_json_case;

import com.alibaba.fastjson.JSON;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public final class BaseController {
    //静态方法封装前端的数据 Class<T> clazz = User.class
    public static <T> T jsonToBean(HttpServletRequest request, Class<T> clazz) throws Exception{
        //将前端传入的Json数据转换成对应的类对象
        T obj = (T) JSON.parseObject(request.getInputStream(), clazz);
        return obj;
    }


    //静态方法响应数据给前端
    public static void sendJsonToBrowser(HttpServletResponse response,Result result) throws Exception{
        //先将对象转化成Json字符串
        String str = JSON.toJSONString(result);
        //传给前端
        response.getWriter().print(str);

    }
}
